        
		function d(str) {
			alert(str);
		}
		
		/* This gets called by the OS container after init is ready and my OS Appl should start up */
        function _wos_initWiiclubOS() {
          d("_wos_initWiiclubOS");
          var req = opensocial.newDataRequest();
          req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.OWNER), 'owner');
          req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.VIEWER), 'viewer');
          req.send(fetchPersonHandler);
        }
        
        /* This gets called when the person data is delivered by the container 
           Defines the GLOBAL vars 
           owner
           viewer
        */
        function fetchPersonHandler(resp) {
          d("fetchPersonHandler");
          var ownerResp = resp.get('owner');    // use the key passed with the request to &quot;get&quot; the appropriate data
 
          if (!ownerResp.hadError()) {          // always verify whether your request was successful
            owner = ownerResp.getData();    // store the <code>opensocial.Person</code> object representing the app's owner
            //alert(owner.getDisplayName());      // retrieve the name using the <code>opensocial.Person</code> object's getDisplayName() method
          }

          var viewerResp = resp.get('viewer');  // use the key passed with the request to &quot;get&quot; the appropriate data
 
          if (!viewerResp.hadError()) {         // always verify whether your request was successful
            viewer = viewerResp.getData();  // store the <code>opensocial.Person</code> object representing the app's owner
            //alert(viewer.getDisplayName());     // retrieve the name using the <code>opensocial.Person</code> object's getDisplayName() method
          }

          // now fetch the clubname
          globalClubname = "";
          var req = opensocial.newDataRequest(); 
          var fields = [ "clubname" ]; 
          var p = {}; 

          p[opensocial.IdSpec.Field.USER_ID] = "OWNER"; 
          var idSpec = opensocial.newIdSpec(p); 
          req.add(req.newFetchPersonAppDataRequest(idSpec, fields), "owner_data"); 
          req.send(fetchClubnameHandler);

        };
        
        function fetchClubnameHandler(data) {
        	d("fetchClubnameHandler");
        	var mydata = data.get("owner_data");
        		
            if (data.hadError()) {
            	globalMessage += data.getErrorMessage();
            	_wos_loadGames();
            }
            
            var ownDataBlock = mydata.getData();
            if (ownDataBlock[owner.getId()]) {
            	var ownData = ownDataBlock[owner.getId()];
            	globalClubname = ownData["clubname"]; 
            }
            
        	_wos_loadGames();
        }
        
        function _wos_loadGames() {
          d("_wos_loadGames");
          var params = {};  
          params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
          params[gadgets.io.RequestParameters.REFRESH_INTERVAL] = 180;  // container should cache result for 180 secs
          var url = "http://www.wiiclub.de/backend/json/usersgames.php?username="+globalClubname;  
          gadgets.io.makeRequest(url, _wos_renderGames, params);
        }
        
        function _wos_setClubname() {
          d("_wos_setClubname");
          var clubname = document.wiicnameimpForm.clubname.value;
          if (clubname == "") {
            alert("Bitte gib Deinen Namen im Wiiclub ein!");
          } else {
            alert("setze "+clubname);
            var req = opensocial.newDataRequest();
            req.add(req.newUpdatePersonAppDataRequest("VIEWER", "clubname", clubname));
            req.send(newClubnameSetEvent);
          }
        }

        // store new clubname in containers OWNER persistance
        function newClubnameSetEvent(data) {
        	d("newClubnameSetEvent");
            if (data.hadError()) {
                globalMessage += '<span class="msg">'+data.getErrorMessage()+'</span>';
              }
            _wos_initWiiclubOS();     // and re-init
        }

        function _wos_renderGames(obj) {
          d('Jetzt _wos_renderGames');
          var jsondata = obj.data;
          var wiicTitle = "Wii Spiele und Bewertungen von <br> "+owner.getDisplayName()+" aka <a href='http://www.wiiclub.de/"+
                  gadgets.util.unescapeString(jsondata.profilelink)+"'>"+
                  jsondata.username+"</a>";
          document.getElementById('wiichead').innerHTML = wiicTitle;
          
          var html = "";
          // prefix global messages
          html += '<span class="msgcontainer">'+globalMessage+'</span>';
          // and reset them 
          globalMessage = '';
          var row = "odd";
          html += "<table id='wiicos1'><thead><tr><th>Spiel</th><th>Punkte</th><th>Code</th></tr></thead><tbody>";
          for ( i in jsondata.games ) {
            if (row == "odd") {
              row = "even" 
            } else {
              row = "odd";
            }
            if (null == jsondata.games[i].gamecode) {
              jsondata.games[i].gamecode = "";
            }
            html += "<tr class='"+row+"'>" + 
                    "<td><a href='http://www.wiiclub.de/"+gadgets.util.unescapeString(jsondata.games[i].link)+
                    "' title='" + jsondata.games[i].title + "'>" + jsondata.games[i].title + "</a></td>" +
                    "<td>" + jsondata.games[i].rating + "</td>" +
                    "<td>" + jsondata.games[i].gamecode + "</td>" +
                    "</tr>";
          }
          html += "</tbody></table>";
          if (jsondata.games.length < 1) {
          }
            
          document.getElementById('wiiclubgames').innerHTML = html;
          var tw = document.getElementById('wiicos1').offsetWidth - 60;
          document.getElementById('wiic2os').style.backgroundPosition = tw+"px 4px";
          //alert("backgroundPosition: "+tw+"px 4px");
        }
        
        
        // GLOBAL 
        globalMessage = '';
        gadgets.util.registerOnLoadHandler(_wos_initWiiclubOS);
        
